package com.anXin.article.controller;


import com.anXin.apis.user.IUserClient;
import com.anXin.article.service.ReplyChildService;
import com.anXin.article.service.ReplyService;
import com.anXin.model.article.dtos.ReplyChildDto;
import com.anXin.model.article.pojos.Reply;
import com.anXin.model.article.pojos.ReplyChild;
import com.anXin.model.common.dtos.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;
import utils.common.DateUtils;
import utils.thread.AppThreadLocalUtil;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/app/replyChild")
public class ReplyChildController {

    @Resource
    private ReplyChildService replyChildService;

    @Lazy
    @Resource
    private ReplyService  replyService;

    @Resource
    private IUserClient userClient;

    /**
     * 查询子级评论√
     * @param parentId
     * @return
     */
    @GetMapping("/{parentId}")
    public ResponseResult findReplyChild(@PathVariable("parentId") Long parentId){
        List<ReplyChild> list=replyChildService.query().eq("parent_id",parentId).list();
        List<ReplyChild> replyList = list.stream().map(reply -> {
            LinkedHashMap<String, Object> data = (LinkedHashMap<String, Object>) userClient.information(reply.getUserId()).getData();
            reply.setAvatar(data.get("image").toString());
            reply.setUsername(data.get("name").toString());
            LinkedHashMap<String, Object> data2 = (LinkedHashMap<String, Object>) userClient.information(reply.getReplyId()).getData();
            reply.setReplyName(data2.get("name").toString());
            return reply;
        }).collect(Collectors.toList());
        return ResponseResult.okResult(replyList);

    }

    /*
    发布子级评论
     */
    @PostMapping("/add")
    public ResponseResult addReplyChild(@RequestBody ReplyChildDto dto){
        ReplyChild replyChild  = new ReplyChild();
        LinkedHashMap<String, Object> data = (LinkedHashMap<String, Object>) userClient.information(AppThreadLocalUtil.getUser().getId()).getData();
        Reply reply = replyService.query().eq("id", dto.getParentId()).one();
        if (reply == null){
            ReplyChild child = replyChildService.query().eq("id", dto.getParentId()).one();
            if (child == null){
                return ResponseResult.errorResult(1,"评论不存在");
            }
            replyChild.setReplyId(child.getUserId());
        }else {
            replyChild.setReplyId(reply.getUserId());
        }
        replyChild.setUsername(data.get("name").toString());
        replyChild.setAvatar(data.get("image").toString());
        replyChild.setContent(dto.getContent());
        replyChild.setParentId(Long.valueOf(dto.getParentId()));
        replyChild.setUserId(AppThreadLocalUtil.getUser().getId());
        replyChildService.save(replyChild);
        return ResponseResult.okResult("回复成功！");
    }


    @DeleteMapping("/delete/{id}")
    public ResponseResult deleteReplyChild(@PathVariable("id") Long id){
        replyChildService.removeById(id);
        return ResponseResult.okResult("删除成功");
    }
}
